package com.itheima.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pojo.Employee;
import com.itheima.service.EmpService;
import com.itheima.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: 放学后海堤日记
 * @Date: 2023/4/13 14:33
 * @Desc: 员工管理
 */
@Slf4j
@RestController
public class EmpController {
    @Autowired
    private EmpService empService;

    /**
     * 查询员工列表
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/employee/page")
    public R<Page<Employee>> listAllEmp(Integer page, Integer pageSize, String name) {
        Page<Employee> employees = empService.listAllEmp(page, pageSize, name);

        return R.success(employees);
    }

    /**
     * 添加员工
     *
     * @param employee
     * @return
     */
    @PostMapping("/employee")
    public R<String> addEmp(@RequestBody Employee employee) {
        int i = empService.addEmp(employee);

        return i != 0 ? R.success("添加成功") : R.error("添加失败");
    }

    /**
     * 修改账号状态
     *
     * @param employee
     * @return
     */
    @PutMapping("/employee")
    public R<String> updateEmpInfo(@RequestBody Employee employee) {
        int i = empService.updateEmpInfo(employee);

        return i != 0 ? R.success("修改成功") : R.error("修改失败");
    }

    /**
     * 根据id查询员工信息
     *
     * @param id
     * @return
     */
    @GetMapping("/employee/{id}")
    public R<Employee> findEmpById(@PathVariable Long id) {
        Employee employee = empService.findEmpById(id);

        return employee != null ? R.success(employee) : R.error("操作失败");
    }
}
